package me.zhengjie.suli.tasks.repository;

import me.zhengjie.suli.tasks.domain.TaskDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TaskDetailRepository extends JpaRepository<TaskDetail, String>, JpaSpecificationExecutor<TaskDetail> {
    @Query(value = "select * from task_detail where task_id = ?1", nativeQuery = true)
    List<TaskDetail> getTaskDetailsByTaskId (String id);

    @Query(value = "select * from task_detail where pk_list in (?1) AND type = '冻结/解冻出库'", nativeQuery = true)
    List<TaskDetail> getTaskDetailsByBoxNumberAndType (List<String> pkList);

    @Query(value = "select * from task_detail where source_id = ?1 AND pk_list = ?2", nativeQuery = true)
    TaskDetail getTaskDetailBySourceIdAndBoxNumber (String sourceId,String boxNumber);

    @Query(value = "select * from task_detail where source_id = ?1", nativeQuery = true)
    List<TaskDetail> getTaskDetailsBySourceId (String sourceId);

    @Query(value = "select top(1)*  from task_detail where pk_list = ?1 AND is_zan_cun = 1 AND status = 1 ORDER BY create_time DESC", nativeQuery = true)
    TaskDetail getTaskDetailByPkListAndIsZanCunAndStatus (String pkList);

    @Query(value = "select * from task_detail where task_code = ?1", nativeQuery = true)
    TaskDetail getTaskDetailByTaskCode (String id);

    @Query(value = "select task_detail.* from task_detail, tasks where tasks.id = task_detail.task_id and task_code = ?1 and task_status = 0 order by create_time desc", nativeQuery = true)
    List<TaskDetail> getTaskDetailsByTaskCode (String taskCode);

    @Query(value = "select * from task_detail where task_id in (?1)", nativeQuery = true)
    List<TaskDetail> getTaskDetailsByTaskIds (List<String> ids);

    @Query(value = "select top(1) * from task_detail where pk_list = ?1 AND type = '立库出到暂存区' AND status = 1", nativeQuery = true)
    TaskDetail getTaskDetailByPkListAndType (String pkList);

    @Query(value = "select top(1) * from task_detail where pk_list = ?1 AND type = '在途物料出货' AND status = 1", nativeQuery = true)
    TaskDetail getTaskDetailByPkListAndTypeOne (String pkList);

    @Query(value = "select * from task_detail where source_id in (?1)  ", nativeQuery = true)
    List<TaskDetail> getTaskDetailsBySourceIds (List<String> ids);

    @Query(value = "select * from task_detail where source_id in (?1) ", nativeQuery = true)
    List<TaskDetail> getTaskDetailsBySourceIdsOne (List<String> ids);

    /** 根据箱号查找TaskDetail */
    @Query(value = "select top(1) task_detail.* from task_detail where pk_list = ?1  AND status != 2 order by create_time desc", nativeQuery = true)
    TaskDetail getTaskDetailsByContainerCode (String pkList);
}
